import { postAction } from '@api/manage'
export const reconsitutionMixin = {
    methods: {
        async initData() {
            // 暂时默认设置
            this.queryParam.gridId = localStorage.getItem('$gridId')
            const res = await postAction('/Sql2ApiCall/call/4028809589b9138c0189b933fa500001', {
                PARAMCODE: 'D',
                PAGECODE: 'root',
            })
            const jsonRes = JSON.parse(res.result)
            this.queryParam.dayId = jsonRes.data.length ? jsonRes.data[0].value : '20230922'
            this.initDataPostHook && this.initDataPostHook(jsonRes)
        },
        setAllRequest(list, params) {
            list.forEach((item, index) => {
                this.getData({ ...item, params })
            })
        },
        async getData({
            url,
            resultParamsName = '',
            requestPostHook = '',
            dataType = 'Object',
            params = {},
            loading = ''
        }) {
            loading && (this[loading] = true)
            const { result } = await postAction('/Sql2ApiCall/call/' + url, params)
            let resultjson = JSON.parse(result)
            const { state, data } = resultjson
            if (state === 200 && data) {
                const returnData = dataType == 'Array' ? data || [] : data[0] || {}
                // 设置对象值
                if (resultParamsName) {
                    if (dataType === 'Object') {
                        // this[resultParamsName] = {}
                        // 设置为默认data数据
                        // commonCorrespondObj && (this[resultParamsName] = { ...DATACONFIG[commonCorrespondObj] })
                        this[resultParamsName] = Object.keys(returnData).length ? { ...returnData } : this[resultParamsName]
                        for (let i in this[resultParamsName]) {
                            if (!this[resultParamsName][i]) {
                                this[resultParamsName][i] = String(0)
                            }
                        }
                    } else {
                        this[resultParamsName] = []
                        this[resultParamsName] = returnData
                    }
                }

                // 请求后置钩子
                if (requestPostHook) {
                    // this.$nextTick(() => {
                    this[requestPostHook](returnData)
                    // })
                }
            }
            loading && (this[loading] = false)
        }
    }

}


